/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.asset.bean;

import com.yunhoo.asset.db.impl.AssetSqlImpl;
import com.yunhoo.asset.model.Asset;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.LoginParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 * 资产审核
 *
 * @author Administrator
 */
@ManagedBean(name = "assetAudit")
@SessionScoped
public class AssetAuditBean {

    private Person auditor;//资产审核人
    private Person assetUser = new Person();//资产使用人
    private Person user = new Person();
    private Department dept = new Department();
    private Department noAuditedDept = new Department();
    private Department audited = new Department();
    private Person noAuditedPsn = new Person();
    private Person auditedPsn = new Person();
    private List<Asset> psnAssets = new ArrayList<Asset>();
    private List<Asset> auditedAssets = new ArrayList<Asset>();
    private List<Asset> psnAssets_scrap = new ArrayList<Asset>();
    private List<Asset> auditedAssets_scrap = new ArrayList<Asset>();
    private boolean all = false;
    private boolean auditedAll = false;
    private Map<String, String> psns = new HashMap<String, String>();
    private Map<String, String> depts = new HashMap<String, String>();
    private List<Department> deptlist = new ArrayList();
    private List<SelectItem> deptList = new ArrayList();
    private List<SelectItem> personList = new ArrayList();
    private Integer fromProcure;//是否来自政采
    private Integer fromProcure_cancel;//是否来自政采

    public AssetAuditBean() {
        auditor = LoginParam.getPer();
        Map map = DeptAndPsnRelation.intiDepartmentInfo();
        depts = (HashMap) map.get("deptMap");
        deptList = (ArrayList<SelectItem>) map.get("deptList");
    }

    public void queryPsnAssetData() {
        DeptAndEmpHelpBean deptEmp = (DeptAndEmpHelpBean) FacesUtils.getManagedBean("deptAndEmp");
        Map map = new HashMap();
        map.put("checkFlag", "0");
        map.put("cpsn_number", noAuditedPsn.getCpsn_number().trim());
        map.put("keeperCode", noAuditedPsn.getCpsn_number().trim());
        map.put("deptcode", deptEmp.getDeptCode());
        map.put("useStatus","'0','3'");
        map.put("fromProcure",fromProcure);
        psnAssets = AssetSqlImpl.psnAssetCardForAudit(map);
    }

    public void queryAuditedPsnAssetData() {
        DeptAndEmpHelpBean deptEmp = (DeptAndEmpHelpBean) FacesUtils.getManagedBean("deptAndEmp");
        Map map = new HashMap();
        map.put("checkFlag", "1");
        map.put("deptcode", deptEmp.getDeptCode1());
        map.put("cpsn_number", auditedPsn.getCpsn_number().trim());
        map.put("keeperCode", auditedPsn.getCpsn_number().trim());
        map.put("useStatus","'0','3'");
        map.put("fromProcure",fromProcure_cancel);
        auditedAssets = AssetSqlImpl.psnAssetCardForAudit(map);
    }
    
    

    public void queryPsnAssetData_scrap() {
        DeptAndEmpHelpBean deptEmp = (DeptAndEmpHelpBean) FacesUtils.getManagedBean("deptAndEmp");
        Map map = new HashMap();
        map.put("checkFlag", "0");
        map.put("cpsn_number", noAuditedPsn.getCpsn_number().trim());
        map.put("keeperCode", noAuditedPsn.getCpsn_number().trim());
        map.put("deptcode", deptEmp.getDeptCode());
        map.put("useStatus","'1','2'");
        map.put("fromProcure",fromProcure);
        psnAssets_scrap = AssetSqlImpl.psnAssetCardForAudit(map);
    }

    public void queryAuditedPsnAssetData_scrap() {
        DeptAndEmpHelpBean deptEmp = (DeptAndEmpHelpBean) FacesUtils.getManagedBean("deptAndEmp");
        Map map = new HashMap();
        map.put("checkFlag", "1");
        map.put("deptcode", deptEmp.getDeptCode1());
        map.put("cpsn_number", auditedPsn.getCpsn_number().trim());
        map.put("keeperCode", auditedPsn.getCpsn_number().trim());
        map.put("useStatus","'1','2'");
        map.put("fromProcure",fromProcure_cancel);
        auditedAssets_scrap = AssetSqlImpl.psnAssetCardForAudit(map);
    }

    public void psnCardsSelected() {
        List<Asset> temp = psnAssets;
        psnAssets = new ArrayList<Asset>();
        if (temp != null && temp.size() > 0) {
            for (int i = 0; i < temp.size(); i++) {
                Asset asset = temp.get(i);
                asset.setCheckFlag(all);
                psnAssets.add(asset);
            }
        }
    }

    public void selectAudited() {
        List<Asset> temp = auditedAssets;
        auditedAssets = new ArrayList<Asset>();
        if (temp != null && temp.size() > 0) {
            for (int i = 0; i < temp.size(); i++) {
                Asset asset = temp.get(i);
                asset.setSelected(auditedAll);
                auditedAssets.add(asset);
            }
        }
    }

    
    public void psnCardsSelected_scrap() {
        List<Asset> temp = psnAssets_scrap;
        psnAssets_scrap = new ArrayList<Asset>();
        if (temp != null && temp.size() > 0) {
            for (int i = 0; i < temp.size(); i++) {
                Asset asset = temp.get(i);
                asset.setCheckFlag(all);
                psnAssets_scrap.add(asset);
            }
        }
    }

    public void selectAudited_scrap() {
        List<Asset> temp = auditedAssets_scrap;
        auditedAssets_scrap = new ArrayList<Asset>();
        if (temp != null && temp.size() > 0) {
            for (int i = 0; i < temp.size(); i++) {
                Asset asset = temp.get(i);
                asset.setSelected(auditedAll);
                auditedAssets_scrap.add(asset);
            }
        }
    }

    
    public void passed() {
        Date checkDate = LoginParam.getLoginDate();
        List<Asset> selected = new ArrayList<Asset>();
        for (Asset card : psnAssets) {
            if (card.isCheckFlag()) {
                selected.add(card);
            }
            card.setAuditor(auditor);
            card.setAuditDate(checkDate);
        }
        AssetSqlImpl.batchAuditPsnAsset(selected);
        queryPsnAssetData();//重新查询未审核的数据
    }

    public void cancelAudited() {
        Date checkDate = LoginParam.getLoginDate();
        List<Asset> selected = new ArrayList<Asset>();
        for (Asset card : auditedAssets) {
            if (card.isSelected()) {
                card.setCheckFlag(false);
                selected.add(card);
            }
            card.setAuditor(null);
            card.setAuditDate(null);
            System.out.println("checked:" + card.isCheckFlag());
        }
        AssetSqlImpl.batchAuditPsnAsset(selected);
        queryAuditedPsnAssetData();
    }
 
    public void passed_scrap() {
        Date checkDate = LoginParam.getLoginDate();
        List<Asset> selected = new ArrayList<Asset>();
        for (Asset card : psnAssets_scrap) {
            if (card.isCheckFlag()) {
                selected.add(card);
            }
            card.setAuditor(auditor);
            card.setAuditDate(checkDate);
        }
        AssetSqlImpl.batchAuditPsnAsset(selected);
        queryPsnAssetData_scrap();//重新查询未审核的数据
    }

    public void cancelAudited_scrap() {
        Date checkDate = LoginParam.getLoginDate();
        List<Asset> selected = new ArrayList<Asset>();
        for (Asset card : auditedAssets_scrap) {
            if (card.isSelected()) {
                card.setCheckFlag(false);
                selected.add(card);
            }
            card.setAuditor(null);
            card.setAuditDate(null);
            System.out.println("checked:" + card.isCheckFlag());
        }
        AssetSqlImpl.batchAuditPsnAsset(selected);
        queryAuditedPsnAssetData_scrap();
    }

    
    public void init() {
        all = false;
    }

    public Person getAssetUser() {
        return assetUser;
    }

    public void setAssetUser(Person assetUser) {
        this.assetUser = assetUser;
    }

    public Person getAuditor() {
        return auditor;
    }

    public void setAuditor(Person auditor) {
        this.auditor = auditor;
    }

    public List<Asset> getPsnAssets() {
        return psnAssets;
    }

    public void setPsnAssets(List<Asset> psnAssets) {
        this.psnAssets = psnAssets;
    }

    public boolean isAll() {
        return all;
    }

    public void setAll(boolean all) {
        this.all = all;
    }

    public List<Asset> getAuditedAssets() {
        return auditedAssets;
    }

    public void setAuditedAssets(List<Asset> auditedAssets) {
        this.auditedAssets = auditedAssets;
    }

    public boolean isAuditedAll() {
        return auditedAll;
    }

    public void setAuditedAll(boolean auditedAll) {
        this.auditedAll = auditedAll;
    }

    public Department getAudited() {
        return audited;
    }

    public void setAudited(Department audited) {
        this.audited = audited;
    }

    public Person getAuditedPsn() {
        return auditedPsn;
    }

    public void setAuditedPsn(Person auditedPsn) {
        this.auditedPsn = auditedPsn;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }

    public List<SelectItem> getDeptList() {
        return deptList;
    }

    public void setDeptList(List<SelectItem> deptList) {
        this.deptList = deptList;
    }

    public List<Department> getDeptlist() {
        return deptlist;
    }

    public void setDeptlist(List<Department> deptlist) {
        this.deptlist = deptlist;
    }

    public Map<String, String> getDepts() {
        return depts;
    }

    public void setDepts(Map<String, String> depts) {
        this.depts = depts;
    }

    public Department getNoAuditedDept() {
        return noAuditedDept;
    }

    public void setNoAuditedDept(Department noAuditedDept) {
        this.noAuditedDept = noAuditedDept;
    }

    public Person getNoAuditedPsn() {
        return noAuditedPsn;
    }

    public void setNoAuditedPsn(Person noAuditedPsn) {
        this.noAuditedPsn = noAuditedPsn;
    }

    public List<SelectItem> getPersonList() {
        return personList;
    }

    public void setPersonList(List<SelectItem> personList) {
        this.personList = personList;
    }

    public Map<String, String> getPsns() {
        return psns;
    }

    public void setPsns(Map<String, String> psns) {
        this.psns = psns;
    }

    public Person getUser() {
        return user;
    }

    public void setUser(Person user) {
        this.user = user;
    } 

    public List<Asset> getPsnAssets_scrap() {
        return psnAssets_scrap;
    }

    public void setPsnAssets_scrap(List<Asset> psnAssets_scrap) {
        this.psnAssets_scrap = psnAssets_scrap;
    }

    public List<Asset> getAuditedAssets_scrap() {
        return auditedAssets_scrap;
    }

    public void setAuditedAssets_scrap(List<Asset> auditedAssets_scrap) {
        this.auditedAssets_scrap = auditedAssets_scrap;
    } 
    
    public Integer getFromProcure() {
        return fromProcure;
    }

    public void setFromProcure(Integer fromProcure) {
        this.fromProcure = fromProcure;
    }

    public Integer getFromProcure_cancel() {
        return fromProcure_cancel;
    }

    public void setFromProcure_cancel(Integer fromProcure_cancel) {
        this.fromProcure_cancel = fromProcure_cancel;
    }
}
